<template>
  <div>
    <el-button type="success" @click="dialogFormVisible=true">发布招聘信息</el-button>
    <!--注册功能-->
    <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="dialogFormVisible" @close="closeDialog('registerInfoRef')">
      <el-form status-icon :model="registerInfo" ref="registerInfoRef"
               label-width="70px">
        <el-form-item label="企业名称" prop="name">
          <el-input type="text" v-model="registerInfo.name"></el-input>
        </el-form-item>
        <el-form-item label="所属行业" prop="industry">
          <el-input  type="text" v-model="registerInfo.industry"></el-input>
        </el-form-item>
        <el-form-item label="招聘职位" prop="job">
          <el-input type="text" v-model="registerInfo.job"></el-input>
        </el-form-item>
        <el-form-item label="招聘薪水" prop="salary">
          <el-input type="text" v-model="registerInfo.salary"></el-input>
        </el-form-item>
        <el-form-item label="工作地点" prop="address">
          <el-input type="text" v-model="registerInfo.address"></el-input>
        </el-form-item>
        <el-form-item label="有效时间" prop="validTime">
          <el-input type="text" v-model="registerInfo.validTime"></el-input>
        </el-form-item>
        <el-form-item label="发布网站" prop="web">
          <el-input type="text" v-model="registerInfo.web"></el-input>
        </el-form-item>
        <el-form-item label="学历" prop="education">
          <el-input type="text" v-model="registerInfo.education"></el-input>
        </el-form-item>
        <el-form-item label="经验" prop="experience">
          <el-input type="text" v-model="registerInfo.experience"></el-input>
        </el-form-item>
        <el-form-item label="招聘人数" prop="number">
          <el-input type="text" v-model="registerInfo.number"></el-input>
        </el-form-item>
        <el-form-item label="职位描述" prop="description">
          <el-input type="text" v-model="registerInfo.description"></el-input>
        </el-form-item>
        <el-form-item label="公司简介" prop="companyProfile">
          <el-input type="text" v-model="registerInfo.companyProfile"></el-input>
        </el-form-item>
      </el-form>
      <div class="register-button" slot="footer">
        <el-button @click="reset('registerInfoRef')">取 消</el-button>
        <el-button type="primary" @click="register('registerInfoRef')">提交修改</el-button>
      </div>
    </el-dialog>
    <h1>职位招聘信息</h1>
    <el-table :data="entrByselfRecruitList" border>
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column prop="name" label="企业名称"></el-table-column>
      <el-table-column prop="industry" label="所属行业"></el-table-column>
      <el-table-column prop="job" label="招聘职位"></el-table-column>
      <el-table-column prop="salary" label="招聘薪水"></el-table-column>
      <el-table-column prop="address" label="工作地点"></el-table-column>
      <el-table-column prop="releaseTime" label="发布时间"></el-table-column>
      <el-table-column prop="validTime" label="有效时间"></el-table-column>
      <el-table-column prop="web" label="发布网站"></el-table-column>
      <el-table-column prop="education" label="学历"></el-table-column>
      <el-table-column prop="experience" label="经验"></el-table-column>
      <el-table-column prop="number" label="招聘人数"></el-table-column>
      <el-table-column prop="description" label="职位描述"></el-table-column>
      <el-table-column prop="companyProfile" label="公司简介"></el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button @click="showEditDialog(scope.row.username)" type="text" size="small">编辑</el-button>
          <el-popconfirm
            confirm-button-text='确定'
            cancel-button-text='算了'
            icon="el-icon-info"
            icon-color="red"
            title="确定要删除这条内容吗？"
            @confirm=deleteInfo(scope.row.id)
          >
            <el-button slot="reference" type="text" size="small">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--    编辑-->
    <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="editDialog">
      <el-form status-icon :model="editForm" ref="editFormRef"
               label-width="70px">
        <el-form-item label="企业名称" prop="name">
          <el-input type="text" v-model="editForm.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="所属行业" prop="industry">
          <el-input  type="text" v-model="editForm.industry"></el-input>
        </el-form-item>
        <el-form-item label="招聘职位" prop="job">
          <el-input type="text" v-model="editForm.job"></el-input>
        </el-form-item>
        <el-form-item label="招聘薪水" prop="salary">
          <el-input type="text" v-model="editForm.salary"></el-input>
        </el-form-item>
        <el-form-item label="工作地点" prop="address">
          <el-input type="text" v-model="editForm.address"></el-input>
        </el-form-item>
        <el-form-item label="发布时间" prop="releaseTime">
          <el-input type="text" v-model="editForm.releaseTime" disabled></el-input>
        </el-form-item>
        <el-form-item label="有效时间" prop="validTime">
          <el-input type="text" v-model="editForm.validTime"></el-input>
        </el-form-item>
        <el-form-item label="发布网站" prop="web">
          <el-input type="text" v-model="editForm.web"></el-input>
        </el-form-item>
        <el-form-item label="学历" prop="education">
          <el-input type="text" v-model="editForm.education"></el-input>
        </el-form-item>
        <el-form-item label="经验" prop="experience">
          <el-input type="text" v-model="editForm.experience"></el-input>
        </el-form-item>
        <el-form-item label="招聘人数" prop="number">
          <el-input type="text" v-model="editForm.number"></el-input>
        </el-form-item>
        <el-form-item label="职位描述" prop="description">
          <el-input type="text" v-model="editForm.description"></el-input>
        </el-form-item>
        <el-form-item label="公司简介" prop="companyProfile">
          <el-input type="text" v-model="editForm.companyProfile"></el-input>
        </el-form-item>
      </el-form>
      <div class="register-button" slot="footer">
        <el-button @click="editDialog=false">取 消</el-button>
        <el-button type="primary" @click="editInfo('editFormRef')">提交修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import Qs from "qs";

  export default {
    name: "entrByselfRecruit",
    data() {
      return {
        entrByselfRecruitList: [],
        username:this.$store.state.username,
        //企业招聘编辑信息对象
        editForm:{},
        //企业招聘编辑对话框开关
        editDialog:false,
        //注册对象
        registerInfo:{
          username:this.$store.state.username,
        },
        //招聘信息注册对话框开关
        dialogFormVisible:false,
      }
    },

    created() {
      this.getentrByselfRecruitList()
    },

    methods:{
      //获取招聘的所有信息
      async getentrByselfRecruitList() {
        let _this = this;
        console.log(_this.username);
        await this.$axios.post('/api/recruitment/byselfAllInfo',Qs.stringify({
          username:_this.username
        })).then((res) => {
          console.log(res);
          if (res.status !== 200) {
            return _this.$message.error("用户数据获取失败")
          }
          _this.entrByselfRecruitList = res.data.data
        })
      },

      //招聘信息注册功能
      register(formName) {
        this.$refs[formName].validate(async valid => {
          if (valid) {
            let _this = this;
            const {data:res} = await this.$axios.post("/api/recruitment/register", Qs.stringify(_this.registerInfo))
            console.log(res);
            if (res.code === 200) {
              console.log(res.message);
              _this.dialogFormVisible = false;
              _this.getentrByselfRecruitList();
              return _this.$message.success(res.message);
            }else{
              return _this.$message.error(res.message);
              console.log(res.message);
            }
          } else {
            return _this.$message.error("用户信息为空,请输入");
          }
        })
      },

      //展示编辑之前的信息
      async showEditDialog(userName){
        console.log(userName);
        const {data:res} = await this.$axios.post("/api/recruitment/getOneInfo", Qs.stringify({username: userName}))
        console.log(res)
        if(res.code!==200){
          this.$message.error("查询用户信息失败")
        }
        this.editForm=res.data
        console.log(res.data)
        this.editDialog=true
      },

      //确认修改方法
      async editInfo(formName){
        this.$refs[formName].validate(async valid => {
          if (!valid) return this.$message.error("参数错误");
          let _this = this;
          const {data: res} = await _this.$axios.put("/api/recruitment/editOneInfo/", Qs.stringify({
            id: _this.editForm.id,
            name: _this.editForm.name,
            industry: _this.editForm.industry,
            job: _this.editForm.job,
            salary: _this.editForm.salary,
            address: _this.editForm.address,
            validTime: _this.editForm.validTime,
            web: _this.editForm.introduce,
            education: _this.editForm.introduce,
            experience: _this.editForm.introduce,
            number: _this.editForm.introduce,
            description: _this.editForm.introduce,
            companyProfile: _this.editForm.companyProfile,
          }))
          console.log(res)
          if (res.code !== 200) {
            return _this.$message.error("数据更新失败!");
          }
          _this.editDialog = false;
          _this.getentrByselfRecruitList();
          _this.$message.success("数据更新成功!")
        })
      },

      //删除一条信息
      async deleteInfo(id) {
        let _this = this;
        console.log(id);
        const {data:res} = await _this.$axios.post("/api/recruitment/deleteInfo",Qs.stringify({id:id}))
        console.log(res)
        if(res.code!==200){
          return _this.$message.error(res.message);
        }
        this.getentrByselfRecruitList();
        return _this.$message.success(res.message);
      },
      //重置表单
      reset(formNameRef) {
        this.$refs[formNameRef].resetFields();
        this.dialogFormVisible = false;
      },
      //右上角X 关闭注册对话框 重置表单
      closeDialog(formNameRef){
        this.$refs[formNameRef].resetFields();
      },
    }
  }
</script>

<style scoped>
  .el-input {
    width: 200px;
  }

  .register-button {
    text-align: center;
  }
</style>
